package com.atguigu.guli.service.trade.controller.api;

import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.base.utils.JwtHelper;
import com.atguigu.guli.service.trade.entity.Order;
import com.atguigu.guli.service.trade.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/trade/order")
public class ApiOrderController {
    @Autowired
    OrderService orderService ;
    //查询订单状态
    @GetMapping("auth/isPay/{orderId}")
    public R isPay(HttpServletRequest request,@PathVariable String orderId) {
        String memberId = JwtHelper.getId(request);
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>() ;
        queryWrapper.eq(Order::getMemberId,memberId) ;
        queryWrapper.eq(Order::getId,orderId) ;
        queryWrapper.eq(Order::getStatus,1) ;
        int count = orderService.count(queryWrapper);
        return R.ok().data("isPay",count>0?"1":"0") ;
    }
    //1.创建订单
    @PostMapping("auth/createOrder/{courseId}")
    public R createOrder(HttpServletRequest request, @PathVariable String courseId) {
        String orderIdRes = orderService.createOrder(request,courseId) ;
        return R.ok().data("id",orderIdRes) ;
    }


    //2.根据orderId回显当前用户的订单
    @GetMapping("auth/getOrder/{orderId}")
    public R getOrder(HttpServletRequest request, @PathVariable String orderId) {
        String memberId = JwtHelper.getId(request);
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Order::getId,orderId) ;
        queryWrapper.eq(Order::getMemberId,memberId) ;
        Order order = orderService.getOne(queryWrapper);
        return R.ok().data("item",order) ;
    }
}
